<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .banner {
            width: 200px;
            height: 300px;
            margin: 40px auto;
            overflow: hidden;
            position: relative;
        }


        .banner ul li {
            width: 200px;
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 50px;
            opacity: 0;
            background-color: #ddd;
            position: absolute;
            top: 0;

            transition: .3s;
        }

        .banner span {
            position: absolute;
            z-index: 3;
        }

        .banner ol {
            display: flex;
            position: absolute;
            bottom: 10px;
        }

        .banner ol li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #0f0;
            margin: 10px;
        }

        .banner span {
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto;
            width: 20px;
            height: 20px;
            background-color: rgba(0, 0, 0, .5);
            color: #fff;
            text-align: center;
            line-height: 20px;
        }

        .banner #next {
            right: 0;
        }


        .banner .show {
            opacity: 1 !important;
        }

        .banner .active {
            background-color: #ff0 !important;
        }
    </style>

    <style>
        .tab ol {
            display: flex;
        }

        .tab ol li {
            width: 100px;
            border: 1px solid #000;
            text-align: center;
        }

        .tab .active {
            color: red;
            border-color: red;
        }

        .tab ul li {
            display: none;
        }

        .tab ul .show {
            display: block;
        }
    </style>
</head>

<body>

    <!-- <div class="tab">
        <ol>
            <li class="dot active">1</li>
            <li class="dot">2</li>
            <li class="dot">3</li>
        </ol>
        <ul>
            <li class="show">11111</li>
            <li>22222</li>
            <li>333333</li>
        </ul>
    </div> -->

    <div class="banner" id="a">
        <span id="prev">&lt;</span>
        <span id="next">&gt;</span>
        <ul id="ul">
            <li class="show">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <ol>
            <li class="dot active"></li>
            <li class="dot"></li>
            <li class="dot"></li>
            <li class="dot"></li>
            <li class="dot"></li>
        </ol>
    </div>

    <script>


        class Tab {
            constructor(ele) {
                this.oTab = document.querySelector(ele);
                this.oOlLis = this.oTab.querySelectorAll('ol li');
                this.oUlLis = this.oTab.querySelectorAll('ul li');  

                // 控制显示哪一个
                this.index = 0;

                this.init();
            }

            init() {
                this.oOlLis.forEach((v, i) => {
                    v.onclick = () => {
                        this.index = i;
                        this.showOne()
                    }
                })
            }

            showOne() {
                // 清除所有
                this.oOlLis.forEach((v, i) => {
                    v.classList.remove('active');
                    this.oUlLis[i].classList.remove('show');
                })
                this.oOlLis[this.index].classList.add('active');
                this.oUlLis[this.index].classList.add('show');
            }
        }





        // class Banner {
        //     constructor(ele){
        //         this.oTab = document.querySelector(ele) ;
        //         this.oOlLis = this.oTab.querySelectorAll('ol li') ;
        //         this.oUlLis = this.oTab.querySelectorAll('ul li') ;
        //         this.init()

        //     }
        //     init(){
        //         this.oOlLis.forEach((v,i) => {
        //             v.onclick = () => {
        //                 // 清除所有
        //                 this.oOlLis.forEach((v,i) => {
        //                     v.classList.remove('active') ;
        //                     this.oUlLis[i].classList.remove('show') ;
        //                 })
        //                 console.log(v);
        //                 v.classList.add('active') ;
        //                 this.oUlLis[i].classList.add('show') ;
        //             }
        //         })
        //     }
        // }



        class Banner extends Tab {
            constructor(ele) {
                super(ele);
                this.oPrev = this.oTab.querySelector('#prev');
                this.oNext = this.oTab.querySelector('#next');
                this.timer = null;
                this.init();
                this.fn();
                this.autoPlay() ;
            }

            fn() {
                this.oPrev.onclick = () => {
                    this.index--;
                    if (this.index === -1) {
                        this.index = this.oOlLis.length - 1;
                    }
                    this.showOne()
                }
                this.oNext.onclick = () => {
                    this.index++;
                    if (this.index === this.oOlLis.length) {
                        this.index = 0;
                    }
                    this.showOne()
                }
            }

            autoPlay() {
                this.timer = setInterval(() => {
                    this.index++;
                    if (this.index === this.oOlLis.length) {
                        this.index = 0;
                    }
                    this.showOne()
                }, 1000)
            }
        }


        new Banner('.banner')
    </script>

</body>

</html>